/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.address.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.IdGenerater;
import com.smooth.dao.gen.BizAddressMapper;
import com.smooth.dao.gen.bean.BaseAreas;
import com.smooth.dao.gen.bean.BaseCities;
import com.smooth.dao.gen.bean.BizAddress;
import com.smooth.dao.gen.bean.BizAddressExample;
import com.smooth.site.sx.mag.basedata.area.service.BaseAreasService;
import com.smooth.site.sx.mag.basedata.city.service.BaseCitiesService;

/**
 * 收货地址Service
 * @author bm
 * @version 2019-02-07
 */
@Service
@Transactional(readOnly = true)
public class BizAddressService{
	Logger log = LoggerFactory.getLogger(BizAddressService.class);
	@Autowired
	BizAddressMapper bizAddressMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public BizAddress get(String no) {
		return bizAddressMapper.selectByPrimaryKey(no);
	}
	
	public List<BizAddress> findList(BizAddress bizAddress) {
		BizAddressExample bizAddressExample = new BizAddressExample();
		BizAddressExample.Criteria criteria = bizAddressExample.createCriteria();
		
		return bizAddressMapper.selectByExample(bizAddressExample);
	}
	
	public Page<BizAddress> findPage(Page<BizAddress> page, BizAddress bizAddress) {
		BizAddressExample bizAddressExample = new BizAddressExample();
		BizAddressExample.Criteria criteria = bizAddressExample.createCriteria();
		
		if(StringUtils.isNotBlank(bizAddress.getUserTelno())) {
			criteria.andUserTelnoEqualTo(bizAddress.getUserTelno());
		}
		if(StringUtils.isNotBlank(bizAddress.getUserName())) {
			criteria.andUserNameLike(" %"+bizAddress.getUserName()+"% ");
		}
		
		bizAddressExample.setPage(page);
		
		List<BizAddress> dlist = bizAddressMapper.selectByExample(bizAddressExample);
		page.setList(dlist);
		page.setCount(bizAddressMapper.countByExample(bizAddressExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public BizAddress save(BizAddress bizAddress) {
		setAddressInfo(bizAddress);
		if(StringUtils.isNotEmpty(bizAddress.getId())&&get(bizAddress.getId())!=null) {
			int optNum = bizAddressMapper.updateByPrimaryKeySelective(bizAddress);
			if(optNum==1) {
				log.info("更新 student 数据【"+bizAddress.toString()+"】成功");
			}else {
				log.error("更新 student 数据【"+bizAddress.toString()+"】失败");
			}
			return bizAddressMapper.selectByPrimaryKey(bizAddress.getId());
		}else {
			Date date = new Date();
			bizAddress.setCreateDate(date);
			bizAddress.setId(idGenerater.genClsNo("bizAddress"));
			bizAddress.setUpdateDate(date);
			int optNum =  bizAddressMapper.insertSelective(bizAddress);
			if(optNum==1) {
				log.info("插入 student 数据【"+bizAddress.toString()+"】成功");
			}else {
				log.error("插入 student 数据【"+bizAddress.toString()+"】失败");
			}
			return bizAddress;
		}
	}
	@Autowired
	BaseCitiesService baseCitiesService;
	@Autowired
	BaseAreasService baseAreasService;
	private void setAddressInfo(BizAddress bizAddress) {
		
		if(StringUtils.isNotBlank(bizAddress.getAreaId())) {
			BaseAreas baseAreas = baseAreasService.get(bizAddress.getAreaId());
			
			bizAddress.setAreaName(baseAreas.getArea());
			bizAddress.setCityId(baseAreas.getCityId());
			bizAddress.setCityName(baseAreas.getCityName());
			
		}
		if(StringUtils.isNotBlank(bizAddress.getCityId())) {
			BaseCities baseCities = baseCitiesService.get(bizAddress.getCityId());
			if(baseCities!=null) {
				bizAddress.setCityId(baseCities.getId());
				bizAddress.setCityName(baseCities.getCityName());
				
				bizAddress.setProvinceId(baseCities.getProvinceId());
				bizAddress.setProvinceName(baseCities.getProvinceName());
			}
		}
		
	}
	@Transactional(readOnly = false)
	public void delete(BizAddress bizAddress) {
		bizAddressMapper.deleteByPrimaryKey(bizAddress.getId());
	}
	
}